package com.cyt.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyt.dao.impl.LiuYanDaoImpl;
import com.cyt.dao.impl.SPFeiLeiDaoImpl;
import com.cyt.entity.LiuYan;
import com.cyt.entity.SPFeiLei;
import com.cyt.util.BaseServlet;
import com.cyt.util.Result;
import com.google.gson.Gson;


public class AdmianLiuYanServlet extends BaseServlet {
	/**
	 * localhost:8080/shiping/admianLiuYan?reqName=getLiuYan
	 * 查询所有的留言
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getLiuYan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LiuYanDaoImpl lYDaoi=new LiuYanDaoImpl();//创建产品分类的实现类对象
	     List <LiuYan> lYLists= lYDaoi.getLiuYan();//查到的所有产品分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(lYLists);//将spLists里面的数据转换为json字符串
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}

	/**
	 *  localhost:8080/shiping/admianLiuYan?reqName=getLiuYanTiao
	 * 查询所有的留言分页 的条数
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getLiuYanTiao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	     PrintWriter out=response.getWriter();
		LiuYanDaoImpl lYDao=new LiuYanDaoImpl();
		String zhuangtai=request.getParameter("zhuangtai");
		 
	     List <LiuYan> lYList= lYDao.getLiuYanZTTiao(zhuangtai);
	    
	     Gson gson=new Gson();
	     String res =gson.toJson(lYList.size());
	     System.out.println(res);

	     out.print(res);
	     
	     out.flush();
	     out.close();
	}
	/**
	 * 分页
	 * 根据状态查留言
	 * localhost:8080/shiping/admianLiuYan?reqName=getLiuYanZT
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getLiuYanZT(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	     PrintWriter out=response.getWriter();
		LiuYanDaoImpl lYDao=new LiuYanDaoImpl();
		String zhuangtai=request.getParameter("zhuangtai");
		 int page=Integer.parseInt(request.getParameter("page"));
		 int size=Integer.parseInt(request.getParameter("size"));
	     List <LiuYan> lYList= lYDao.getLiuYanZT(zhuangtai, page, size);
	    
	     Gson gson=new Gson();
	     String res =gson.toJson(lYList);
	     
	     out.print(res);
	     
	     out.flush();
	     out.close();
	}
	
	
	/**
	 * localhost:8080/shiping/admianLiuYan?reqName=getLiuYanId
	 * 根据id查留言 回显
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getLiuYanId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	     PrintWriter out=response.getWriter();
		LiuYanDaoImpl lYDao=new LiuYanDaoImpl();
		int luiYanId=Integer.parseInt(request.getParameter("luiYanId"));
		
	    LiuYan liuYan= lYDao.getLiuYanId(luiYanId);
	    
	     Gson gson=new Gson();
	     String res =gson.toJson(liuYan);
	     
	     out.print(res);
	     
	     out.flush();
	     out.close();
	}
	/**
	 * 修改留言状态 已读
	 * localhost:8080/shiping/admianLiuYan?reqName=xiuGaiLiuYanZT
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiLiuYanZT(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	     PrintWriter out=response.getWriter();
		LiuYanDaoImpl lYDao=new LiuYanDaoImpl();
		int luiYanId=Integer.parseInt(request.getParameter("luiYanId"));
		
	    LiuYan liuyan=lYDao.getLiuYanId(luiYanId);
	    if(liuyan.getZhuangtai().equals("已读") ||liuyan.getZhuangtai().equals("已回复") ) {
			out.print(Result.toClient(true, "不需要修改", ""));

	    }else {
	    	 liuyan =new LiuYan(luiYanId, "",null);
	 	    int n= lYDao.xiuGaiLiuYanZT(liuyan);
	 	    if (n>0) {//删除成功
	 			out.print(Result.toClient(true, "修改成功", ""));
	 		} else {//删除失败
	 			out.print(Result.toClient(false, "修改失败", ""));

	 		}
	    }
	   
	     out.flush();
	     out.close();
	}
	
	/**
	 * 修改留言状态 已回复  保存回复
	 * admianLiuYan?reqName=xiuGaiLiuYan
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiLiuYan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	     PrintWriter out=response.getWriter();
		LiuYanDaoImpl lYDao=new LiuYanDaoImpl();
		int luiYanId=Integer.parseInt(request.getParameter("luiYanId"));
		String huifu=request.getParameter("huifu");

	    LiuYan liuyan=new LiuYan(luiYanId, "", huifu);
	    
	    int n= lYDao.xiuGaiLiuYan(liuyan);
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "回复成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "回复失败", ""));

		}
	     out.flush();
	     out.close();
	}
	
	/**
	 * localhost:8080/shiping/admianLiuYan?reqName=shanChuLiuYan
	 * 删除留言
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuLiuYan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    int liuYanId=Integer.parseInt(request.getParameter("categoryId"));//接收前台传来的参数
	    LiuYanDaoImpl lYDaoi=new LiuYanDaoImpl();//创建产品分类的实现类对象
	    int n =lYDaoi.shanLiuYan(liuYanId);//调用删除的方法,获得数据库受影响行数n
	    response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * localhost:8080/shiping/admianLiuYan?reqName=shanChuLiuYanSS
	 * 删除留言
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuLiuYanSS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();

		String liuYanIds=request.getParameter("liuYanIds");
	    LiuYanDaoImpl lYDaoi=new LiuYanDaoImpl();
	    int n =lYDaoi.shanLiuYanSS(liuYanIds);
	    if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}

}
